﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using Service;
using MODEL;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using System.Windows.Input;
using Microsoft.Practices.ServiceLocation;
using GalaSoft.MvvmLight.Views;
using GalaSoft.MvvmLight.Ioc;
using Common;
using System.ComponentModel;


namespace ViewModel.SystemManagement
{
    public class UserRoleManageViewModel:ViewModelBase
    {
        public ICommand UpdateUserRoleCommand { get; set; }

        public ICommand ClearOtherRoleCheckCommand { get; set; }

        private User m_User;
        public User User
        {
            get { return m_User; }
            set { m_User = value; base.RaisePropertyChanged(); }
        }

        private IEnumerable<Role> m_RoleList;
        public IEnumerable<Role> RoleList
        {
            get { return this.m_RoleList; }
            set { this.m_RoleList = value; RaisePropertyChanged(() => this.RoleList); }
        }

        private IRoleService m_RoleService;

        public UserRoleManageViewModel(IRoleService roleService)
        {
            this.m_RoleService = roleService;
            this.UpdateUserRoleCommand = new RelayCommand(UpdateUserRole);
            this.ClearOtherRoleCheckCommand = new RelayCommand<Role>(ClearOtherRoleCheck);

            RefreshData(() => this.PropertyChanged += OnPropertyChange);
        }

        public async void RefreshData(Action afterRefreshAction = null)
        {
            
            this.RoleList = await this.m_RoleService.QueryRole();

            if (afterRefreshAction != null)
            {
                afterRefreshAction.Invoke();
            }
        }

        private void OnPropertyChange(object sender, PropertyChangedEventArgs e)
        {
            if ("User" == e.PropertyName)
            {
                if (this.User == null || this.User.role == null)
                {
                    return;
                }
                Role selectRole = this.RoleList.FirstOrDefault(role => role.ID == this.User.role.ID);
                if (selectRole != null)
                {
                    selectRole.IsChecked = true;
                }
            }
        }

        public void ClearOtherRoleCheck(Role selectedRole)
        {
            if (selectedRole.IsChecked)
            {
                this.RoleList.Where(role => role != selectedRole).ToList().ForEach(
                    role => role.IsChecked = false
                    );
            }
        }

        public async void UpdateUserRole()
        {
            if (this.RoleList.All(role => !role.IsChecked))
            {
                await ServiceLocator.Current.GetInstance<IDialogService>().ShowMessage("请选中一个角色", "校验失败");
                return;
            }

            if (this.User.role == null)
            {
                await ServiceLocator.Current.GetInstance<IDialogService>().ShowMessage("该用户没有角色信息", "校验失败");
                return;
            }

            try
            {
                Task task = this.m_RoleService.UpdateUserRole(this.User.Id, this.RoleList.First(role => role.IsChecked).ID);
                task.Wait();
                await ServiceLocator.Current.GetInstance<IDialogService>().ShowMessage("保存成功", "提示");
                this.MessengerInstance.Send<object>(null, "UserRoleManager_CloseForm");
            }
            catch (Exception ex)
            {
                await ServiceLocator.Current.GetInstance<IDialogService>().ShowError($"发送失败:{ex.Message}", "错误", "确定", null);
                Log.WriteException(ex);
            }
        }
    }
}
